package gui.product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import plugin.ChainManager;
import core.Product;
import core.ProductFacade;
import core.exception.ModelException;
import gui.common.*;

/**
 * Controller class for the add item view.
 */
//NOTE: the description field is currently set to be enabled upon loading of values
//		when add functionality to get description for barcode from the web, need to 
//		change this to be disabled and to display a loading message until the 
//		description has been retrieved from the web
public class AddProductController extends ProductController implements IAddProductController 
{	
	
	private boolean attemptedIdentify;
	private Timer idTimer = new Timer(200,
			new ActionListener() {
				public void actionPerformed(ActionEvent a) {
					idProduct();
				}
			}
	);
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the add product view
	 * @param barcode Barcode for the product being added
	 */
	public AddProductController(IView view, String barcode) 
	{
		//no preconditions;
		super(view, new ProductData(barcode));
		_pFacade = ProductFacade.instance();
		_pView = (ProductView)view;
		attemptedIdentify = false;
		construct();
	}

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IAddProductView getView() 
	{
		return (IAddProductView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		if(!attemptedIdentify && !idTimer.isRunning()) {
			getView().setDescription("Identifying Product, please wait...");
			getView().enableDescription(false);
			idTimer.start();
		}
		else {
			_pView.enableDescription(true);
		}
		_pView.enableOK(false);
		_pView.enableBarcode(false);
		_pView.enableShelfLife(true);
		_pView.enableSizeUnit(true);
		_pView.enableSizeValue(false);
		_pView.enableSupply(true);
	}

	/**
	 * This method is called when the user clicks the "OK"
	 * button in the add product view.
	 */
	@Override
	public void addProduct() 
	{
		String barcode = _pData.getBarcode();
		String desc = _pData.getDescription();
		String value = _pData.getSizeValue();
		String unit = _pData.getSizeUnit();
		int shelfLife = 0;
		int supply = 0;
		Product testProduct = null;
		
		try {
			shelfLife = Integer.parseInt(_pData.getShelfLife());
			supply = Integer.parseInt(_pData.getSupply());
		}
		catch (NumberFormatException e) {
			getView().displayErrorMessage("Non-numeric data");
			return;
		} 
		
		if (Product.canCreate(barcode, desc, value, unit, shelfLife, supply)) {
			testProduct = new Product(barcode, desc, value, unit, shelfLife, supply);
			try {
				_pFacade.addProduct(testProduct);
			}
			catch (ModelException e) {
				e.printStackTrace();
				_view.displayErrorMessage("Error adding " + _pData.getDescription());
			}
		}
		else {
			_view.displayErrorMessage("Invalid product data!");
		}
		
	}
	
	private void idProduct() {
		String barcode = getView().getBarcode();
		ChainManager cm = new ChainManager();
		String desc = cm.identifyProduct(barcode);
		getView().setDescription(desc);
		_pData.setDescription(desc);
		if(!(getView().getDescription().isEmpty())) {
			getView().enableOK(true);
		}
		else {
			getView().enableOK(false);
		}
		getView().enableDescription(true);
		attemptedIdentify = true;
		idTimer.stop();
	}

}

